package com.qifan.energy.widget.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.IEventCallback;
import com.qifan.energy.R;

public class VersionUpdateDialog extends DialogFragment {

    private TextView titleTv;
    private IEventCallback callback = new IEventCallback() {

        @Override
        public void onCancel() {

        }

        @Override
        public void onComfirm() {

        }
    };

    /* 显示 */
    public VersionUpdateDialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    /* 对外提供 */
    public VersionUpdateDialog setEventCallback(IEventCallback callback) {
        this.callback = callback;
        return this;
    }

    /* 设置标题 */
    public VersionUpdateDialog title(String text){
        titleTv.setText(text);
        return this;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // 设置主题的构造方法
        // AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_version_update, null);
        builder.setView(view);
        findView(view);
        return builder.create();
    }

    /* 设置View */
    private void findView(View view) {
        titleTv = view.findViewById(R.id.title_tv);
        ImageView closeIv = view.findViewById(R.id.close_iv);
        TextView confirmTv = view.findViewById(R.id.confirm_tv);
        closeIv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onCancel();
        });
        confirmTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onComfirm();
        });
    }
}
